<?php
namespace app\admin\controller;

use think\Request;

use think\Session;

use think\Db;
use think\App;

class Huijiao extends Base
{
	//批量轮播图上传
	public function List_img_uploads($name,$sk,$request)
	{
		$path = 'public' . DS;
		$paths = 'uploads' . DS . $sk . DS . 'admin' . '/';
		$img['0']=request()->file($name.'1');
		$img['1']=request()->file($name.'2');
		$img['2']=request()->file($name.'3');
		foreach ($img as $key=>$file) {
			if(!empty($file)){
				$info = $file
				->move(ROOT_PATH . $path . $paths);
				if ($info) {
					$imgs[$key] = $paths . $info->getSaveName();
				} else {
					err('', $file->getError());
				}
			}else{
				$imgs[$key]="";
			}
		}
		//返回数据
		$mg = json_encode($imgs);
		return $mg;
	}
	
	//分解轮播
	public function resolve_imgs($images,$new_image)
	{
		$images= json_decode($images);      //查询数据库有图片的情况下，转换成json格式
		$new_image=json_decode($new_image);  //把新图片替换成旧图片
		//图片替换操作
		if(!empty($images)){
			foreach ($new_image as $key=>$value) {
				//判断图片不为空的情况下修改
				if($new_image[$key]!=null && $new_image!=""){
					$images[$key]=$new_image[$key];
				}else{//保持原有图片格式
				}
			}
		}else{
			$images=$new_image; //如果图片为空的话就直接用新图片替换成旧图片
		}
	
		$images=json_encode($images);
		return $images;
	}
	
	// 首页框架
    public function index()
    {
        if ( ! session('?user_id')) {
            $this->redirect('admin/huijiao/login');
        }
        
        return $this->fetch();
    }
    
    // 登录
    public function login()
    {
        if(request()->isPost()) {

            $post = request()->only(['account', 'pwd'], 'post');

            if (empty($post['account'])) {
                err('请输入账号');
            }
            if (empty($post['pwd'])) {
                err('请输入密码');
            }
            
            $result = db('customer')->where(['username' => $post['account']])
                ->field("id, mobile, username, name, pwd, status, expire, rules")
                ->find();

            // 查找不到
            if(! $result) {
                err('该账号不存在');
            }
            
            if($result['status'] != 1) {
                err('账号已禁用，请联系管理员');
            }
 
            // 密码不正确
            if(md5($post['pwd']) != $result['pwd']) {
                err('密码错误，请重新输入');
            }

            $rules = explode(',', $result['rules']);
            session('user_id', $result['id']);
            session("user_account", $result['username']);
            session('user_name', $result['name']);
            session('user_auth', $rules);

            // 验证成功
            suc('验证成功');
        }
        
        // 默认登录失败
        return $this->fetch();
    }
    
    // 登出
    public function logout()
    {
        session('user_id', null);
        session('user_account', null);
        session('user_name', null);
        session('user_auth', null);
        return $this->redirect('admin/huijiao/login');
    }
    
    // 欢迎页面
    public function welcome()
    {
        return $this->fetch();
    }

    ##用户列表
    public function user_list()
    {
        $post = request()->only(['name'],'post');
        
        if (!empty($post['name'])){
            $where['nickname'] = ['like','%'.$post['name'].'%'];
            $where['admin_id'] = $this->accountId;
        }else{
            $where['admin_id'] = $this->accountId;
        }
        
        $res = db('user')->where($where)->field('id,avatarurl,nickname,country,province')->select();
        
        $this->assign('res',$res);
        return $this->fetch();
    }

    ##分配权限
    public function user_edit($id)
    {
        //所有学校
        $school = db('huijiao_school')->field('id,name')->select();

        $this->assign('school',$school);
        $this->assign('user_id',$id);
        return $this->fetch();
    }
    
    ##分配提交
    public function user_edit_post($user_id)
    {
        $post = request()->only(['school_id','name','tel','type'],'post');
        
        $type = ['1','2'];
        if (in_array($post['type'], $type)){
            //查询是否还存在
            $info = db('huijiao_zhiwei')->where(['school_id'=>$post['school_id'],'type'=>$post['type']])->find();
            
            if ($info){
                $this->error('对不起！当前权限已经分配了');
            }
        }
 
        
        $post['admin_id'] = $this->accountId;
        $post['user_id'] = $user_id;
        $post['create_time'] = date("Y-m-d");
        
        $res = db('huijiao_zhiwei')->insert($post);
        
        if ($res) {
            $this->success('分配成功','huijiao/user_list');
        }else{
            $this->error('分配失败');
        }
    }
    
    ##管理员列表
    public function adminuser_list()
    {
        $post = request()->only(['school_id'],'post');
        
        if (!empty($post['school_id'])){
            $where['school_id'] = $post['school_id'];
        }else{
            $where = [];
        }
        $res = db('huijiao_zhiwei')->where($where)->select();
        
        foreach ($res as $k => $v){
            if ($v['type'] == 1){
                $res[$k]['type'] = "推广设置(机构管理)";
            }elseif ($v['type'] == 2){
                $res[$k]['type'] = "校区总代理";
            }elseif ($v['type'] == 3){
                $res[$k]['type'] = "老师/学生代理";
            }elseif ($v['type'] == 4){
                $res[$k]['type'] = "机构";
            }
        }
        
        //所有学校
        $school = db('huijiao_school')->field('id,name')->select();
        
        $this->assign('school',$school);
        $this->assign('res',$res);
        
        return $this->fetch();
    }
    
    ##管理员编辑页面
    public function adminuser_edit($id)
    {
        $res = db('huijiao_zhiwei')->where(['id'=>$id])->find();
        
        $this->assign('res',$res);
        return $this->fetch();
    }
    
    ##管理员编辑提交
    public function adminuser_edit_post($id)
    {
        $post = request()->only(['name','tel'],'post');
        
        $res = db('huijiao_zhiwei')->where(['id'=>$id])->update($post);
        
        if ($res) {
            $this->success('操作成功','huijiao/adminuser_list');
        }else{
            $this->error('操作失败');
        }
        
    }
    
    ##管理员删除
    public function adminuser_del($id)
    {
        $res = db('huijiao_zhiwei')->delete($id);
        
        if ($res) {
            $this->success('操作成功','huijiao/adminuser_list');
        }else{
            $this->error('操作失败');
        }
    }
    
    ##学校列表
    public function school_list()
    {
    	//机构信息
    	$field = "id,image,name,tel,tuition,relation_id,state,sort,create_time";
        $res = db('huijiao_school')->field($field)->order('create_time desc')->select();

        $info= db('huijiao_school')->field('id,name')->select();
        //机构类型
        $type = db('huijiao_type')->select();
        
        foreach ($res as $k =>$v){
        	foreach ($info as $kk => $vv){
        		if ($v['relation_id'] == $vv['id']){
        			$res[$k]['relation_id'] = $vv['name'];
        		}
        	}
        	
        	if ($v['sort'] == 1){
        		$res[$k]['sort'] = "推荐";
        	}else{
        		$res[$k]['sort'] = "不推荐";
        	}
        	
        	foreach ($type as $kk => $vv){
        		if ($v['state'] == $vv['id']){
        			$res[$k]['state'] = $vv['name'];
        		}
        	}
        	
        }
        
        $this->assign('res', $res);
        return $this->fetch();
    }

    ##学校添加页面
    public function school_add()
    {
        $relation_id = db('huijiao_school')->field('id,name')->select();
        $state = db('huijiao_type')->select();
        
        $this->assign('state', $state);
        $this->assign('relation_id', $relation_id);
        return $this->fetch();
    }

    ##学校添加提交
    public function school_add_post(Request $request)
    {
    	$post = request()->only(['tuition',
    			'state','relation_id','sort','name','content',
    			'tel','longitude','latitude','area','address','key_word'
    	],'post');
    	
    	$file = new \app\admin\logic\File();
        $post['image'] = $file->uploadFileAdmin($this->accountId);
    	$post['img'] = $this->List_img_uploads('img',$this->accountId,$request);
    	$post['admin_id'] = $this->accountId;
    	$post['create_time'] = date("Y-m-d H:i:s");
    	
    	
    	$res = db('huijiao_school')->insertGetId($post);
    	
    	if ($res) {
            //增加推广设置
            $data['admin_id'] = $this->accountId;
            $data['school_id'] = $res;
            $data['create_time'] = date("Y-m-d H:i:s");
            db('huijiao_extension')->insert($data);
            $this->success('操作成功','huijiao/school_list');
        }else{
            $this->error('操作失败');
        }
    }
    
    ##学校编辑页面
    public function school_edit($id)
    {
    	$res = db('huijiao_school')->where(['id'=>$id])->find();
    	$res['img'] = json_decode($res['img']);
    	if (!isset($res['img'][0])){
    	    $res['img'][0] = "";
    	}elseif (!isset($res['img'][1])){
    	    $res['img'][1] = "";
    	}elseif (!isset($res['img'][2])){
    	    $res['img'][2] = "";
    	}
    
    	$relation_id = db('huijiao_school')->field('id,name')->select();
    	$state = db('huijiao_type')->select();
    	
    	$this->assign('state', $state);
    	$this->assign('relation_id', $relation_id);
    	$this->assign('res', $res);
    	return $this->fetch();
    }
    
    ##学校编辑提交
    public function school_edit_post(Request $request)
    {
    	$post = request()->only(['id','tuition',
    			'state','relation_id','sort','name','content',
    			'tel','longitude','latitude','area','address','key_word'
    	],'post');
    	 
    	$file = new \app\admin\logic\File();
    	$image = $file->uploadFileAdmin($this->accountId);
    	if (!empty($image)){
            $post['image'] = $image;
        }
    	
    	$post['img'] = $this->List_img_uploads('img',$this->accountId,$request);
    	if (!empty($post['img'])){
    		$images = db('huijiao_school')->where(['id'=>$post['id']])->value('img');
    		$resolve_imgs = $this->resolve_imgs($images,$post['img']);
    		$post['img'] = $resolve_imgs;
    	}
    	
    	$post['create_time'] = date("Y-m-d H:i:s");
    	 
    	 
    	$res = db('huijiao_school')->where(['id'=>$post['id']])->update($post);
    	 
    	if ($res) {
    		$this->success('操作成功','huijiao/school_list');
    	}else{
    		$this->error('操作失败');
    	}
    }
    
    ##学校删除
    public function school_del($id)
    {
    	$res = db('huijiao_school')->delete($id);
    	
    	if ($res) {
    		$this->success('操作成功','huijiao/school_list');
    	}else{
    		$this->error('操作失败');
    	}
    }
    
    ##机构入驻列表
    public function admission_list()
    {
        //机构类型
        $arr = db('huijiao_type')->select();
        $res = db('huijiao_school_apply')->order('create_time desc')->select();
        
        foreach ($res as $k => $v){
            foreach ($arr as $key => $val){
                if ($v['type'] == $val['id']){
                    $res[$k]['type'] = $val['name'];
                }
            }
        }
        
        
        $this->assign('res',$res);
        return $this->fetch();
    }
    
    ##机构入驻审核页面
    public function admission_edit($id)
    {
        $res = db('huijiao_school_apply')->field('id,state')->where(['id'=>$id])->find();
        
        $this->assign('res',$res);
        return $this->fetch();
    }
    
    ##机构入驻审核提交
    public function admission_edit_post($id)
    {
        $post = request()->only(['state'],'post');

        $res = db('huijiao_school_apply')->where(['id'=>$id])->update(['state'=>$post['state']]);
    
    if ($res) {
    		$this->success('操作成功','huijiao/admission_list');
    	}else{
    		$this->error('操作失败');
    	}
    }
    
    ##文章列表
    public function article_list()
    {
    	$post = request()->only(['type'],'post');

        if (empty($post['type'])) {
            $where = [];
        }else{
            $where['school_id'] = $post['type'];
        }


    	$res = db('huijiao_article')->where($where)->order('create_time desc')->select();
    	
        //所有机构
        $arr = db('huijiao_school')->field('id,name')->select();

        $this->assign('arr',$arr);
    	$this->assign('res',$res);
    	return $this->fetch();
    }
    
    ##文章添加页面
    public function article_add()
    {
        //所有机构
        $arr = db('huijiao_school')->field('id,name')->select();
        
        $this->assign('arr',$arr);
    	return $this->fetch();
    }
    
    ##文章添加提交
    public function article_add_post()
    {
    	$post = request()->only(['title','introduction','content','key_word','name','school_id'],'post');
    	
    	$file = new \app\admin\logic\File();
    	$post['img'] = $file->uploadFileAdmin($this->accountId);
    	$post['mv'] = $file->uploadFileAdmin($this->accountId);
    	$post['admin_id'] = $this->accountId;
    	$post['create_time'] = date("Y-m-d H:i:s");
    	##假数据
    	$post['zhiwei_id'] = 3;
    	
    	$res = db('huijiao_article')->insert($post);
    	
    	if ($res) {
    		$this->success('操作成功','huijiao/article_list');
    	}else{
    		$this->error('操作失败');
    	}
    }
    
    ##文章编辑页面
    public function article_edit($id)
    {
    	$res = db('huijiao_article')->where(['id'=>$id])->find();
    	
    	$this->assign('res',$res);
    	return $this->fetch();
    }
    
    ##文章编辑提交
    public function article_edit_post($id)
    {
    	$post = request()->only(['title','introduction','content','key_word','name'],'post');
    	
    	$file = new \app\admin\logic\File();
    	$img = $file->uploadFileAdmin($this->accountId);
    	
    	if (!empty($img)){
    		$post['img'] = $img;
    	}
    	
    	$mv = $file->uploadFileAdmin($this->accountId);
    	
    	if (!empty($mv)){
    		$post['mv'] = $mv;
    	}
    	
    	$res = db('huijiao_article')->where(['id'=>$id])->update($post);
    	
    	if ($res) {
    		$this->success('操作成功','huijiao/article_list');
    	}else{
    		$this->error('操作失败');
    	}
    }
    
    ##文章删除
    public function article_del($id)
    {
    	$res = db('huijiao_article')->delete($id);
    	
    	if ($res) {
    		$this->success('操作成功','huijiao/article_list');
    	}else{
    		$this->error('操作失败');
    	}
    }
    
    ##课程列表
    public function curriculum_list()
    {
        $post = request()->only(['type'],'post');

        if (empty($post['type'])) {
            $where = [];
        }else{
            $where['school_id'] = $post['type'];
        }


        $res = db('huijiao_curriculum')->where($where)->order('create_time desc')->select();
        
        //所有机构
        $arr = db('huijiao_school')->field('id,name')->select();

        $this->assign('arr',$arr);
        $this->assign('res',$res);
        return $this->fetch();
    }
    
    ##增加课程页面
    public function curriculum_add()
    {
        //所有机构
        $arr = db('huijiao_school')->field('id,name')->select();
        
        $this->assign('arr',$arr);
        return $this->fetch();
    }
    
    ##增加课程提交
    public function curriculum_add_post()
    {
        $post = request()->only(['name','content','original_price','price','school_id'],'post');

        $file = new \app\admin\logic\File();
        $post['img'] = $file->uploadFileAdmin($this->accountId);
        $post['admin_id'] = $this->accountId;
        $post['create_time'] = date("Y-m-d H:i:s");

        $res = db('huijiao_curriculum')->insert($post);
        
        if ($res) {
            $this->success('操作成功','huijiao/curriculum_list');
        }else{
            $this->error('操作失败');
        }
    }

    ##课程编辑页面
    public function curriculum_edit($id)
    {
        $res = db('huijiao_curriculum')->where(['id'=>$id])->find();

        $this->assign('res',$res);
        return $this->fetch();
    }

    ##课程编辑提交
    public function curriculum_edit_post($id)
    {
        $post = request()->only(['name','content','original_price','price'],'post');;
        
        $file = new \app\admin\logic\File();
        $img = $file->uploadFileAdmin($this->accountId);
        
        if (!empty($img)){
            $post['img'] = $img;
        }
        
        
        $res = db('huijiao_curriculum')->where(['id'=>$id])->update($post);
        
        if ($res) {
            $this->success('操作成功','huijiao/curriculum_list');
        }else{
            $this->error('操作失败');
        }
    }

    ##课程删除
    public function curriculum_del($id)
    {
        $res = db('huijiao_curriculum')->delete($id);
        
        if ($res) {
            $this->success('操作成功','huijiao/curriculum_list');
        }else{
            $this->error('操作失败');
        }
    }
    
    ##咨询预约列表
    public function consultation_list()
    {
        $post = request()->only(['school_id'],'post');
        
        if (!empty($post['school_id'])){
            $where['school_id'] = $post['school_id'];
        }else{
            $where['school_id'] = ['neq',""];
        }
        $res = db('huijiao_consultation')->where($where)->order(['create_time desc'])->select();
        
        foreach ($res as $k => $v){
            if ($v['state'] == 0){
                $res[$k]['state'] = "未查看";
            }else{
                $res[$k]['state'] = "已查看";
            }
            
            if ($v['type'] == 1){
                $res[$k]['type'] = "自由";
            }else{
                $res[$k]['type'] = "推荐";
            }
        }
        
        //所有机构
        $school = db('huijiao_school')->field('id,name')->select();
        
        $this->assign('school',$school);
        $this->assign('res',$res);
        return $this->fetch();
    }
    
    ##推广设置
    public function config_list()
    {
        $post = request()->only(['school_id'],'post');

        if (empty($post['school_id'])) {
            $where = [];
        }else{
            $where['school_id'] = $post['school_id'];
        }

        $res = db('huijiao_extension')->where($where)->find();

        $arr = db('huijiao_school')->field('id,name')->select();

        $this->assign('arr',$arr);
        $this->assign('res',$res);
        return $this->fetch();
    }

    ##推广设置提交
    public function config_edit_post($id,$cid)
    {
        $post = request()->only(['school_id','read_money','sign_money','recommend_money','free_read_money',
            'free_sign_money','state','distance','recommend_see','balance','income'.'purchase'
            ],'post');

        $res = db('huijiao_extension')->where(['id'=>$id])->update($post);

        if ($res) {
            $this->success('操作成功','huijiao/config_list');
        }else{
            $this->error('操作失败');
        }
    }

    ##机构类型
    public function type_list()
    {
        $res = db('huijiao_type')->order('create_time desc')->select();

        $this->assign('res',$res);
        return $this->fetch();
    }

    ##机构类型增加页面
    public function type_add()
    {
        return $this->fetch();
    }

    ##机构类型增加提交
    public function type_add_post()
    {
        $post = request()->only(['name'],'post');

        $post['admin_id'] = $this->accountId;
        $post['create_time'] = date("Y-m-d H:i:s");

        $res = db('huijiao_type')->insert($post);

        if ($res) {
            $this->success('操作成功','huijiao/type_list');
        }else{
            $this->error('操作失败');
        }
    }

    ##机构类型编辑页面
    public function type_edit($id)
    {
        $res = db('huijiao_type')->where(['id'=>$id])->find();

        $this->assign('res',$res);
        return $this->fetch();
    }

    ##机构类型类型提交
    public function type_edit_post($id)
    {
        $post = request()->only(['name'],'post');

        $post['create_time'] = date("Y-m-d H:i:s");

        $res = db('huijiao_type')->where(['id'=>$id])->update($post);

        if ($res) {
            $this->success('操作成功','huijiao/type_list');
        }else{
            $this->error('操作失败');
        }
    }

    ##名师列表
    public function teacher_list()
    {
        $post = request()->only(['type'],'post');

        if (empty($post['type'])) {
            $where = [];
        }else{
            $where['school_id'] = $post['type'];
        }


        $res = db('huijiao_teacher')->where($where)->order('create_time desc')->select();
        
        //所有机构
        $arr = db('huijiao_school')->field('id,name')->select();

        $this->assign('arr',$arr);
        $this->assign('res',$res);
        return $this->fetch();
    }

    ##名师增加页面
    public function teacher_add()
    {
        //所有机构
        $arr = db('huijiao_school')->field('id,name')->select();
        
        $this->assign('arr',$arr);
        return $this->fetch();
    }

    ##名师增加页面提交
    public function teacher_add_post()
    {
        $post = request()->only(['name','title','school_id'],'post');

        $file = new \app\admin\logic\File();
        $post['img'] = $file->uploadFileAdmin($this->accountId);

        $post['admin_id'] = $this->accountId;
        $post['create_time'] = date("Y-m-d H:i:s");

        $res = db('huijiao_teacher')->insert($post);

        if ($res) {
            $this->success('操作成功','huijiao/teacher_list');
        }else{
            $this->error('操作失败');
        }
    }

    ##名师编辑页面
    public function teacher_edit($id)
    {
        $res = db('huijiao_teacher')->where(['id'=>$id])->find();

        $this->assign('res',$res);
        return $this->fetch();
    }

    ##名师编辑页面提交
    public function teacher_edit_post($id)
    {
        $post =request()->only(['name','time','title'],'post');

        $file = new \app\admin\logic\File();
        $img = $file->uploadFileAdmin($this->accountId);

        if (!empty($img)) {
            $post['img'] = $img;
        }

        $post['create_time'] = date("Y-m-d H:i:s");

        $res = db('huijiao_teacher')->where(['id'=>$id])->update($post);

        if ($res) {
            $this->success('操作成功','huijiao/teacher_list');
        }else{
            $this->error('操作失败');
        }
    }

    ##名师删除
    public function teacher_del($id)
    {
        $res = db('huijiao_teacher')->delete($id);

        if ($res) {
            $this->success('操作成功','huijiao/teacher_list');
        }else{
            $this->error('操作失败');
        }
    }


    ##推荐列表
    public function recommend_list()
    {
        $post = request()->only(['type'],'post');

        if (empty($post['type'])) {
            $where = [];
        }else{
            $where['school_id'] = $post['type'];
        }


        $res = db('huijiao_recommend')->where($where)->order('create_time desc')->select();
        foreach ($res as $key => $value) {
            if ($value['state'] == 0) {
                $res[$key]['state'] = "未查看";
            }elseif ($value['state'] == 1) {
                $res[$key]['state'] = "已查看";
            }
        }

        //所有机构
        $arr = db('huijiao_school')->field('id,name')->select();

        $this->assign('arr',$arr);
        $this->assign('res',$res);
        return $this->fetch();
    }

    ##推荐生删除
    public function recommend_del($id)
    {
        $res = db('huijiao_recommend')->delete($id);
        if ($res) {
            $this->success('操作成功','huijiao/recommend_list');
        }else{
            $this->error('操作失败');
        }
    }


    /**
     * 业务员列表
     */
    ##省级代理列表
    public function SalesmanList()
    {
        $where['province_id'] = 0;      //省级代理
        $where['city_id'] = 0;
        $res = db('huijiao_salesman')->where($where)->paginate($this->page, false);
        
        $page = $res->render();
        $this->assign('page', $page);
        $this->assign('res',$res);
        return $this->fetch();
    }
    
    ##增加省级代理页面
    public function SalesmanAdd()
    {
        return $this->fetch();
    }
    
    ##编辑省级代理页面
    public function SalesmanEdit($id)
    {
        $res = db('huijiao_salesman')->where(['id'=>$id])->find();
        $this->assign('res',$res);
        return $this->fetch();

    }
    
    ##市级代理列表
    public function CityList($id)
    {
        $where['province_id'] = $id;      //市级代理
        $where['city_id'] = 0;
        $res = db('huijiao_salesman')->where($where)->paginate($this->page, false);

        $page = $res->render();
        $this->assign('page', $page);
        $this->assign('res',$res);
        $this->assign('city_id',$id);
        return $this->fetch();
    }

    ##增加市级代理页面
    public function CityAdd($city_id)
    {
        $this->assign('city_id',$city_id);
        return $this->fetch();
    }
    
    ##编辑市级代理页面
    public function CityEdit($id)
    {
        $res = db('huijiao_salesman')->where(['id'=>$id])->find();
        $this->assign('res',$res);
        return $this->fetch();
    
    }
    
    ##区级代理列表
    public function AreaList($id,$city_id=0)
    {
        $where['city_id'] = $id;
        $res = db('huijiao_salesman')->where($where)->paginate($this->page, false);
        
        $page = $res->render();
        $this->assign('page', $page);
        $this->assign('res',$res);
        $this->assign('city_id',$city_id);
        $this->assign('area_id',$id);
        return $this->fetch();
    }

    
    ##增加市级代理页面
    public function AreaAdd($area_id)
    {
        $this->assign('area_id',$area_id);
        return $this->fetch();
    }

    ##编辑区级代理页面
    public function Area($id)
    {
        $res = db('huijiao_salesman')->where(['id'=>$id])->find();
        $this->assign('res',$res);
        return $this->fetch();
    
    }
    
    ##增加代理提交
    public function SalesmanPost($type,$province_id=0)
    {
        $post = request()->only(['name','tel','province'],'post');
        
        $post['create_time'] = date("Y-m-d H:i:s");
        if ($type == 1){
            $post['province_id'] = 0;
            $res = db('huijiao_salesman')->insert($post);
        }elseif ($type == 2){
            $post['province_id'] = $province_id;
            $post['city_id'] = 0;
            $res = db('huijiao_salesman')->insert($post);
        }elseif ($type == 3){
            $post['city_id'] = $province_id;
            $res = db('huijiao_salesman')->insert($post);
        }
        
        if ($res) {
            $this->success('操作成功','huijiao/SalesmanList');
            
        }else{
            $this->error('操作失败');
        }
    }

    ##编辑代理提交
    public function SalesmanPost_edit($type,$id)
    {
        $post = request()->only(['name','tel','province'],'post');
    
        $post['create_time'] = date("Y-m-d H:i:s");

        $where['id'] = $id;
        $res = db('huijiao_salesman')->where($where)->update($post);

    
        if ($res) {
            $this->success('操作成功','huijiao/SalesmanList');
    
        }else{
            $this->error('操作失败');
        }
    }

    ##删除代理提交
    public function SalesmanDel($id)
    {

        $res = db('huijiao_salesman')->delete($id);
    
    
        if ($res) {
            $this->success('操作成功','huijiao/SalesmanList');
    
        }else{
            $this->error('操作失败');
        }
    }






    
    
    
    
    
    
    
   
    
    
}